import {policy} from "../../../api/oss";
import {getInfo,getRealName} from "../../../api/user";
import {sendMsg, realName } from '../../../api/user'
let app = getApp();
const util = require('../../../utils/util.js')
Page({
  data: {
    maxlength:20,
    addBtnFlag:true,
    btnText:'提交实名',
    realNameForm: {
      cardName: '',
      mobile: '',
      idCardNo: '',
      bankCardNo: '',
      imgCardFront: '',
      imgCardBack: '',
      verifyCode: ''
    },
    imgCardFront: 'http://rzico-weex.oss-cn-hangzhou.aliyuncs.com/People365/shenfenzhengzhengmian.png',
    imgCardBack: 'http://rzico-weex.oss-cn-hangzhou.aliyuncs.com/People365/shenfenzhengfanmian.png',
    dataObj: {
      policy: '',
      signature: '',
      key: '',
      ossaccessKeyId: '',
      dir: '',
      host: ''
    },
    value: 0,
    uuid: '',
    images: [],
    imgType: '',
    second: 60,
    isShow: true,
    loading:false,
    realNameStatus:''
  },
  onLoad: function (options) {
    this.beforeUpload()
    this.getPersonalInformation()
  },
  metaClick(e){
    this.data.realNameForm.imgCardFront = e.detail.images[0]
  },
  metaClickTwo(e){
    this.data.realNameForm.imgCardBack = e.detail.images[0]
  },
  getCardNameInput(e) {
    this.data.realNameForm.cardName =  e.detail.value
    this.setData({
      ['realNameForm.cardName']: this.data.realNameForm.cardName
    })
  },
  getIdCardNoInput(e){
    this.data.realNameForm.idCardNo =  e.detail.value
    this.setData({
      ['realNameForm.idCardNo']: this.data.realNameForm.idCardNo
    }) 
  },
  getbankCardNoInput(e){
    this.data.realNameForm.bankCardNo =  e.detail.value
    this.setData({
      ['realNameForm.bankCardNo']: this.data.realNameForm.bankCardNo
    }) 
  },
  getMobileInput (e){
    this.data.realNameForm.mobile =  e.detail.value
    this.setData({
      ['realNameForm.mobile']: this.data.realNameForm.mobile
    }) 
    console.log(this.data.realNameForm,"999999")
  },
  getVerifyCodeInput(e){
    this.data.realNameForm.verifyCode =  e.detail.value
    this.setData({
      ['realNameForm.verifyCode']: this.data.realNameForm.verifyCode
    }) 
  },

  getPersonalInformation(){

    getRealName().then(res=>{
      if(res.data.realNameStatus == null){
        this.setData({
          realNameStatus : res.data.realNameStatus
        })
        return false
      }

      let realNameInfo = JSON.parse(res.data.realNameInfo)
      if(realNameInfo.idCardNo){
        this.data.realNameForm.idCardNo= realNameInfo.idCardNo.replace(/^(.{4})(?:\d+)(.{4})$/,"$1****$2");
        this.setData({
          ['realNameForm.idCardNo']: this.data.realNameForm.idCardNo
        })
      }
      if(realNameInfo.mobile){
        this.data.realNameForm.mobile= realNameInfo.mobile.replace(/^(.{4})(?:\d+)(.{4})$/,"$1******$2");
        this.setData({
          ['realNameForm.mobile']: this.data.realNameForm.mobile
        })
      }
      if(realNameInfo.bankCardNo){
        this.data.realNameForm.bankCardNo = realNameInfo.bankCardNo.replace(/^(.{4})(?:\d+)(.{4})$/,"$1****$2");
        this.setData({
          ['realNameForm.bankCardNo']: this.data.realNameForm.bankCardNo
        })
      }
      this.data.realNameForm.cardName = realNameInfo.cardName
      this.data.realNameForm.imgCardFront = realNameInfo.imgCardFront
      this.data.realNameForm.imgCardBack = realNameInfo.imgCardBack
      this.setData({
        ['realNameForm.cardName']: this.data.realNameForm.cardName,
        ['realNameForm.imgCardFront']: this.data.realNameForm.imgCardFront,
        ['realNameForm.imgCardBack']: this.data.realNameForm.imgCardBack,
      })
      this.data.btnText = '已实名'
      this.data.addBtnFlag = false
      this.setData({
        btnText: this.data.btnText ,
        addBtnFlag:this.data.addBtnFlag
      })
    })
  },
  beforeUpload() {
    let _self = this;
    policy({mchId: app.globalData.mchId}).then(response => {
      console.log(this.getGlobalData().mchId)
      _self.dataObj.policy = response.data.policy;
      _self.dataObj.signature = response.data.signature;
      _self.dataObj.ossaccessKeyId = response.data.accessKeyId;
      _self.dataObj.dir = response.data.dir;
      _self.dataObj.key = response.data.dir + '/' + _self.uuid + "${filename}";
      _self.dataObj.host = response.data.action;
      _self.host = response.data.host
    }).catch(err => {
      console.log(err)
    })
  },
  pick(data) {
    this.imgType = data
    picker.pick({
      items: ['本地上传', '拍照上传']
    }, event => {
      if (event.result === 'success') {
        this.value = event.data
        this.fileClick()
      }
    })
  },
  fileClick() {
    var _this = this
    _this.uuid = this.getUUID()
    if (this.value == 0) {
      picture.create({
        type: 'gallery',
        gallery: 1,
        mode: 1
      }, function (result) {
        if (result.status == 'success') {
          console.log(result)
          var tempFilePath = result.lists[0].path
          _this.uploadImg(tempFilePath)
        }
      });
    } else if (this.value == 1) {
      picture.create({
        type: 'camera',
        gallery: 1,
        mode: 1
      }, function (result) {
        if (result.status == 'success') {
          console.log(result)
          var tempFilePath = result.lists[0].path
          _this.uploadImg(tempFilePath)
        }
      });
    }
  },
  uploadImg(filePath) {
    let _this = this;
    var typePath = filePath;
    console.log(filePath)
    _this.typePath = typePath.split('.')
    _this.dataObj.key = _this.dataObj.dir + '/' + _this.uuid + '.' + _this.typePath[_this.typePath.length - 1];
    console.log(_this.dataObj)
    console.log(filePath)
    network.upload({
      url: _this.dataObj.host,
      path: filePath,
      name: 'file',
      formData: _this.dataObj,
    }, (ret) => {
      if (ret.status == 200 || ret.status == 204) {
        let avatar = _this.dataObj.host + '/' + _this.dataObj.dir + '/' + _this.uuid + '.' + _this.typePath[_this.typePath.length - 1]
        if (_this.imgType == 'just') {
          _this.realNameForm.imgCardFront = avatar
        } else {
          _this.realNameForm.imgCardBack = avatar
        }
      }
      if (ret.status != 100) {
      }
    })
  },
  timer: function () {
    let promise = new Promise((resolve, reject) => {
      let setTimer = setInterval(() => {
            this.data.second = this.data.second - 1
            if (this.data.second <= 0) {
              this.data.second = 60;
              this.data.isShow = true;
              this.setData({
                isShow: this.data.isShow
              })
              resolve(setTimer)
            }
            this.setData({
              second: this.data.second
            })
          }, 1000)
    });
    promise.then((setTimer) => {
      clearInterval(setTimer)
    });
  },
  verificationClick() {
    var _this = this
    console.log(this.data.realNameForm.mobile.length)
    if (this.data.isShow == false) {
      return;
    }
    console.log(this.data.realNameForm.mobile,"1321321")
    if (this.data.realNameForm.mobile == "" || this.data.realNameForm.mobile.length != 11 || !(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$/.test(this.data.realNameForm.mobile))) {
      wx.showToast({
        title: '请输入正确手机号码',
        icon: 'none',
      })
      return;
    }
    console.log(app.globalData.mchId)
    sendMsg({
      username: _this.data.realNameForm.mobile,
      tplKey: 'register',
      mchId: app.globalData.mchId,
    }).then(res => {
      this.timer()
      this.data.isShow = false
      this.setData({
        isShow: this.data.isShow
      })
      console.log(res)
    }).catch(error => {
      console.log(error)
    })
  },
  realNameClick() {
    var _this = this
    if (util.isNull(_this.data.realNameForm.cardName)) {
      wx.showToast({
        icon: 'none',
        title: '请输入您的姓名！',
      })
      return;
    }
    if (this.data.realNameForm.mobile == "" || this.data.realNameForm.mobile.length != 11 || !(/^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\d{8}$/.test(this.data.realNameForm.mobile))) {
      wx.showToast({
        icon: 'none',
        title: '请输入正确的电话号码！',
      })
      return;
    }
    if (util.isNull(_this.data.realNameForm.idCardNo)) {
      wx.showToast({
        icon: 'none',
        title: '请输入您的身份证号！',
      })
      return;
    }
    // if (util.isNull(_this.data.realNameForm.bankCardNo)) {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '请输入您的银行卡号！',
    //   })
    //   return;
    // }
    if (util.isNull(_this.data.realNameForm.imgCardFront)) {
      wx.showToast({
        icon: 'none',
        title: '请选择您的身份证正面！',
      })
      return;
    }
    if (util.isNull(_this.data.realNameForm.imgCardBack)) {
      wx.showToast({
        icon: 'none',
        title: '请选择您的身份证反面！',
      })
      return;
    }
    if (util.isNull(_this.data.realNameForm.verifyCode)) {
      wx.showToast({
        icon: 'none',
        title: '请输入您的验证码！',
      })
      return;
    }

    realName(_this.data.realNameForm).then(res=>{
      wx.showToast({
        icon: 'none',
        title: '添加成功',
      })
      wx.navigateBack({
        delta: 1
      })
    }).catch(error=>{
      wx.showToast({
        icon: 'none',
        title: '保存失败',
      })
    })
  }

})